layui.use(['layer', 'form', 'table', 'ztree', 'laydate', 'admin', 'ax'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;

    var layer = layui.layer;
    var $ZTree = layui.ztree;
    var laydate = layui.laydate;

    /**
     * 商品信息表管理
     */
    var HxBasicGoods = {
        tableId: "hxBasicGoodsTable",    //表格id
        condition: {
            goodsBigtypedictId: "",
        }
    };

    /**
     * 初始化表格的列
     */
    HxBasicGoods.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {align: 'center', toolbar: '#tableBar', title: '图片', width: 60},
            {field: 'goodsName', sort: true, title: '名称', width: 160},
            {field: 'goodsNumber', sort: true, title: '编号'},
            {field: 'goodsPcode69', sort: true, title: '商品码'},
            {field: 'goodsSpecs', sort: true, title: '规格'},
            {field: 'jibenUnit', sort: true, title: '基本单位'},
            {field: 'goodsWarranty', sort: true, title: '保质期'},
        ]];
    };

    /**
     * 选择时
     */
    HxBasicGoods.onClickDept = function (e, treeId, treeNode) {
        HxBasicGoods.condition.goodsBigtypedictId = treeNode.id;
        HxBasicGoods.search();
    };

    /**
     * 点击查询按钮
     */
    HxBasicGoods.search = function () {
        var queryData = {};
        queryData['goodsCustomerId'] = $("#goodsCustomerId").val();
        queryData['condition'] = $("#condition").val();
        queryData['goodsBigtypedictId'] = HxBasicGoods.condition.goodsBigtypedictId;
        table.reload(HxBasicGoods.tableId, {
            where: queryData, page: {curr: 1},
             done:function () {
            }
        });
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + HxBasicGoods.tableId,
        url: Feng.ctxPath + '/hxBasicGoods/list',
        page: true,
        limit: 40,
        height: "full-60",
        cellMinWidth: 100,
        cols: HxBasicGoods.initColumn()
    });
    
    $('#assign').click(function () {
    	var checkStatus = table.checkStatus('hxBasicGoodsTable');
        data = checkStatus.data;
        if (data.length>0) {
            var gids = [];
            for (var i = 0; i < data.length; i++) {
            	gids.push(data[i].id)
			}
            layer.confirm('确定绑定商品吗?', function(index){
            	layer.close(index);
                $.ajax({
                	url:Feng.ctxPath + "/hxBasicPositionGoods/addItem?gids="+gids+"&positionId="+Feng.getUrlParam("positionId"),
                	cache: false,
                	dataType:"json",
                	beforeSend:function(){
                    	//弹出加载中
                		index = layer.load(1, {
                            shade: [0.3,'#000'] //0.1透明度的白色背景
                        });
                	},
                	success:function(data){
                    	layer.close(index); 
                		if (data.success) {
                        	var Indexs = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(Indexs);
                        	Feng.success("绑定成功!");
						} else {
                            Feng.error("绑定失败！" + data.message)
						}
                	},
                	error:function(data){
                    	layer.close(index); 
                        Feng.error("绑定失败！" + data.responseJSON.message)
                	}
                })
            }); 
		} else {
			layer.alert('请至少选中一条数据');
		}
    });
    
    //回填商品数据
    function confirm(data){
        var gids = [];
    	gids.push(data.id)
    	var index;
        $.ajax({
        	url:Feng.ctxPath + "/hxBasicPositionGoods/addItem?gids="+gids+"&positionId="+Feng.getUrlParam("positionId"),
        	cache: false,
        	dataType:"json",
        	beforeSend:function(){
            	//弹出加载中
        		index = layer.load(1, {
                    shade: [0.3,'#000'] //0.1透明度的白色背景
                });
        	},
        	success:function(data){
            	layer.close(index); 
        		if (data.success) {
                	var Indexs = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(Indexs);
                	Feng.success("绑定成功!");
				} else {
                    Feng.error("绑定失败！" + data.message)
				}
        	},
        	error:function(data){
            	layer.close(index); 
                Feng.error("绑定失败！" + data.responseJSON.message)
        	}
        })
    }
    
    // 双击行事件
    table.on('rowDouble(' + HxBasicGoods.tableId + ')', function (obj) {
        var data = obj.data;
    	confirm(data);
    });
    
    // 工具条点击事件
    table.on('tool(' + HxBasicGoods.tableId + ')', function (obj) {
        var data = obj.data;
        if (obj.event === 'paymentProve') {
  	    	//构建图像数组
      		var items = [];
        	if (data.v1!='') {
        		var obj = {};
        		obj.src = '/system/previewa/' + data.v1;
        		obj.title = '预览图片';
        		items.push(obj)
    	      	//定义选项（如果需要）
    	      	var options = {
    	      	    index: 0 // 此选项表示您将从第一张图片开始
    	      	};
    	      	//初始化插件
    	      	var viewer = new PhotoViewer(items, options);
			} else {
                Feng.info("暂无照片!");
			}
        } 
    });

    //初始化左侧部门树
    var ztree = new $ZTree("deptTree", "/hxBasicGoodsCode/treeo");
    ztree.bindOnClick(HxBasicGoods.onClickDept);
    ztree.init();

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        HxBasicGoods.search();
    });

    //返回按钮
    $("#backupPage").click(function () {
    	var Index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(Index);
    });

});
